package com.example.zluserservice.common.util;

import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;

import java.util.Collection;
import java.util.Properties;

/**
 * @Author zhenliu（孙凌岳）
 * @Description 用户表范围分片算法
 * @Date 2025/6/14 15:26
 * @Version 1.0
 */
public class UserRangeShardingAlgorithm implements StandardShardingAlgorithm<Long> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        //分表逻辑示例：根据ID范围分片
        long id = shardingValue.getValue();
        String tableSuffix;

        if (id >= 1_000_000L && id < 2_000_000L) {
            tableSuffix = "1";
        } else if (id >= 2_000_000L && id < 3_000_000L) {
            tableSuffix = "2";
        } else {
            tableSuffix = "3";
        }

        return shardingValue.getLogicTableName() + "_" + tableSuffix;
    }

    //重写其他方法

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        return null;
    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return null;
    }
}